package com.blog.Bard.base.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.blog.Bard.base.pojo.BardMenu;
import com.blog.Bard.base.service.BardMenuService;
import com.blog.Bard.common.controller.BaseController;
import com.blog.Bard.common.util.Pager;

/**
 * 	菜单管理
 * @author phil
 *
 */
@Controller
@RequestMapping("/base/bardmenu")
public class BardMenuController extends BaseController {
	
	@Autowired
	private BardMenuService bardMenuService;
	
	/**
	 * 获取左侧菜单数据
	 * @param req
	 * @param res
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getMenuList")
	public Map<String,Object> getMenuList(HttpServletRequest req, HttpServletResponse res){
		logger.info("execute the BardMenuController 's getMenuList method...");
		Map<String,Object> map = new HashMap<String, Object>();
		List<Map<String,Object>> data = null;
		try {
			data = bardMenuService.getMenuList();
			map = super.getSuccessResultMap("ok", data, data.size());
		} catch (Exception e) {
			e.printStackTrace();
			map = super.getErrorResultMap("fail");
		}
		return map;
	}
	
	/**
	 * 获取菜单列表
	 * @param req
	 * @param res
	 * @return
	 */
	@ResponseBody
    @RequestMapping(value = "/menuList")
    public Map<String,Object> menuList(HttpServletRequest req, HttpServletResponse res){
    	logger.info("execute the BardMenuController 's menuList method...");
    	Map<String,Object> map = new HashMap<String, Object>();
    	Map<String,Object> params = new HashMap<String, Object>();
    	try {
			int pageNo = super.getInt(req, "page", 1);
			int pageSize = super.getInt(req, "limit", 10);
			int status = super.getInt(req, "status", -1);
			String searchValue = super.getString(req, "searchValue", "");
			params.put("status", status);
			params.put("searchValue", searchValue);
			Pager pager = bardMenuService.getMenuPager(params, pageNo, pageSize);
			map = super.getSuccessResultMap("ok", pager.getData(),pager.getTotalRecord());
		} catch (Exception e) {
			e.printStackTrace();
			map = super.getErrorResultMap("fail");
		}
        return map;
    }
    
    /**
     * 	菜单管理列表页面
     * @param model
     * @return
     */
    @RequestMapping("/list")
	public String list(Model model) {
    	logger.info("execute the BardMenuController 's list method...");
		return "menu/list";
	}
    
    /**
     * 	获取菜单树数据
     * @param req
     * @param res
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getMenuTree")
    public Map<String,Object> getMenuTree(HttpServletRequest req, HttpServletResponse res){
    	logger.info("execute the BardMenuController 's getMenuTree method...");
    	Map<String,Object> map = new HashMap<String, Object>();
    	List<Map<String, Object>> result = null;
    	try {
    		result = bardMenuService.getMenuTree();
    		if(result!=null) {
    			map = super.getSuccessResultMap("ok", result);    			
    		}else {
    			map = super.getErrorResultMap("fail");
    		}
		} catch (Exception e) {
			e.printStackTrace();
			map = super.getErrorResultMap("fail");
		}
        return map;
    }
    
    /**
     * 根据id获取实体
     * @param req
     * @param res
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getOneById")
    public Map<String,Object> getOneById(HttpServletRequest req, HttpServletResponse res){
    	logger.info("execute the BardMenuController 's getOneById method...");
    	Map<String,Object> map = new HashMap<String, Object>();
    	try {
    		Integer id = super.getInt(req, "id", 0);
    		BardMenu entity = bardMenuService.getOneById(id);
    		if(entity!=null) {
    			map = super.getSuccessResultMap("ok", entity);
    		}else {
    			map = super.getErrorResultMap("fail");
    		}
		} catch (Exception e) {
			e.printStackTrace();
			map = super.getErrorResultMap("fail");
		}
        return map;
    }
    
    /**
     * 	查找所有一级菜单
     * @param req
     * @param res
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getParentMenuList")
    public Map<String,Object> getParentMenuList(HttpServletRequest req, HttpServletResponse res){
    	logger.info("execute the BardMenuController 's getParentMenuList method...");
    	Map<String,Object> map = new HashMap<String, Object>();
    	try {
    		List<BardMenu> list = bardMenuService.getParentMenuList();
    		if(list!=null) {
    			map = super.getSuccessResultMap("ok", list);
    		}else {
    			map = super.getErrorResultMap("fail");
    		}
		} catch (Exception e) {
			e.printStackTrace();
			map = super.getErrorResultMap("fail");
		}
        return map;
    }
    
    /**
     * 	保存
     * @param req
     * @param res
     * @param t
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save")
    public Map<String,Object> save(HttpServletRequest req, HttpServletResponse res, BardMenu t){
    	logger.info("execute the BardMenuController 's save method...");
    	Map<String,Object> map = new HashMap<String, Object>();
    	try {
    		if(t!=null) {
    			bardMenuService.save(t);
    			map = super.getSuccessResultMap("ok", null);
    		}else {
    			map = super.getErrorResultMap("fail");
    		}
		} catch (Exception e) {
			e.printStackTrace();
			map = super.getErrorResultMap("fail");
		}
        return map;
    }
    
    /**
     * 	删除菜单
     * @param req
     * @param res
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/del")
    public Map<String,Object> del(HttpServletRequest req, HttpServletResponse res){
    	logger.info("execute the BardMenuController 's del method...");
    	Map<String,Object> map = new HashMap<String, Object>();
    	try {
    		Integer id = super.getInt(req, "id", 0);
    		if(id!=0) {
    			bardMenuService.del(id);
    			map = super.getSuccessResultMap("ok", null);
    		}else {
    			map = super.getErrorResultMap("fail");
    		}
		} catch (Exception e) {
			e.printStackTrace();
			map = super.getErrorResultMap("fail");
		}
        return map;
    }

}
